home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / KK4S0V (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  2.0 KB  |  70 lines

  1. package java.io;
  2.  
  3. public class ByteArrayOutputStream extends OutputStream {
  4.    protected byte[] buf;
  5.    protected int count;
  6.  
  7.    public ByteArrayOutputStream() {
  8.       this(32);
  9.    }
  10.  
  11.    public ByteArrayOutputStream(int size) {
  12.       this.buf = new byte[size];
  13.    }
  14.  
  15.    public synchronized void reset() {
  16.       this.count = 0;
  17.    }
  18.  
  19.    public int size() {
  20.       return this.count;
  21.    }
  22.  
  23.    public synchronized byte[] toByteArray() {
  24.       byte[] newbuf = new byte[this.count];
  25.       System.arraycopy(this.buf, 0, newbuf, 0, this.count);
  26.       return newbuf;
  27.    }
  28.  
  29.    public String toString() {
  30.       return new String(this.buf, 0, this.count);
  31.    }
  32.  
  33.    /** @deprecated */
  34.    public String toString(int hibyte) {
  35.       return new String(this.buf, hibyte, 0, this.count);
  36.    }
  37.  
  38.    public String toString(String enc) throws UnsupportedEncodingException {
  39.       return new String(this.buf, 0, this.count, enc);
  40.    }
  41.  
  42.    public synchronized void write(byte[] b, int off, int len) {
  43.       int newcount = this.count + len;
  44.       if (newcount > this.buf.length) {
  45.          byte[] newbuf = new byte[Math.max(this.buf.length << 1, newcount)];
  46.          System.arraycopy(this.buf, 0, newbuf, 0, this.count);
  47.          this.buf = newbuf;
  48.       }
  49.  
  50.       System.arraycopy(b, off, this.buf, this.count, len);
  51.       this.count = newcount;
  52.    }
  53.  
  54.    public synchronized void write(int b) {
  55.       int newcount = this.count + 1;
  56.       if (newcount > this.buf.length) {
  57.          byte[] newbuf = new byte[Math.max(this.buf.length << 1, newcount)];
  58.          System.arraycopy(this.buf, 0, newbuf, 0, this.count);
  59.          this.buf = newbuf;
  60.       }
  61.  
  62.       this.buf[this.count] = (byte)b;
  63.       this.count = newcount;
  64.    }
  65.  
  66.    public synchronized void writeTo(OutputStream out) throws IOException {
  67.       out.write(this.buf, 0, this.count);
  68.    }
  69. }
  70.